﻿Imports System.Xml
Imports Mysoft.WebControls
Imports System.IO
Imports Mysoft.Map.Data
Imports System.Data

Public Class Activity_Index
    Inherits Mysoft.Map.Application.Security.AppPage

    Protected _presentationHtml As String
    Dim myCstGroupDB As New CstGroupSettingDB
    Protected strMode As String

    Public Sub New()
        Me._presentationHtml = String.Empty
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not IsPostBack Then
            '获取应用程序类型
            txtAppName.Text = Request.QueryString("open")
            Try
                txtFuncid.Text = IIf(Request.QueryString("funcid").Substring(0, 4) = "0101", "01010204", Request.QueryString("funcid"))
            Catch ex As Exception
                txtFuncid.Text = "01010204"
            End Try

            '新增内容模板按钮的权限点控制
            If Mysoft.Map.Application.Security.User.CheckUserRight(Session("userguid"), "01010110", "51") And txtAppName.Text.ToLower = "slxt" Then
                txtIsCreate.Text = "1"
            End If

            If Mysoft.Map.Application.Security.User.CheckUserRight(Session("userguid"), "01040110", "51") And txtAppName.Text.ToLower = "zlxt" Then
                txtIsCreate.Text = "1"
            End If

            If Mysoft.Map.Application.Security.User.CheckUserRight(Session("userguid"), "01030203", "51") And txtAppName.Text.ToLower = "hyxt" Then
                txtIsCreate.Text = "1"
            End If

            If Mysoft.Map.Application.Security.User.CheckUserRight(Session("userguid"), "01010110", "51") And Request.QueryString("funcid") = "01010204" Then
                txtIsCreate.Text = "1"
            End If

            If Mysoft.Map.Application.Security.User.CheckUserRight(Session("userguid"), "01040110", "51") And Request.QueryString("funcid") = "01040305" Then
                txtIsCreate.Text = "1"
            End If

            If Mysoft.Map.Application.Security.User.CheckUserRight(Session("userguid"), "01030203", "51") And Request.QueryString("funcid") = "01030303" Then
                txtIsCreate.Text = "1"
            End If

            '接受参数
            '新增或者修改
            txtMode.Text = Request.QueryString("mode")
            If txtMode.Text = "1" Then
                strMode = "新增"
                txtExecDate.Value = Now.ToString("yyyy-MM-dd")
                txtBeginDate.Value = Now.ToString("yyyy-MM-dd")
            Else
                strMode = "修改"
            End If

            '生产执行计划GUID

            If txtMode.Text = "1" Then
                '是否存在Guid参数
                If Not Request.QueryString("Guid") Is Nothing Then
                    txtActExecGUID.Text = Request.QueryString("Guid")
                    txtIsOutIn.Text = "1"
                Else
                    txtActExecGUID.Text = Guid.NewGuid().ToString()
                End If

                '判断是否从活动通知过来的页面
                If Not Request.QueryString("MAGUID") Is Nothing Then
                    txtMAGUID.Text = Request.QueryString("MAGUID")
                End If

                '发送类型
                txtActType.Text = Request.QueryString("ActType")

                If txtActType.Text = "短信" Then
                    crmHtmlBar.Options = HtmlBar.HtmlBarParam.CutCopyPaste
                End If

                RdoSearchType1.Checked = True

                '制定人姓名
                txtExecutor.Text = Session("UserName")
                txtUserGUID.Text = Session("UserGUID")
                txtBUGUID.Text = Session("BUGUID")
                ddlPriority.SelectedValue = "一般"

                '系统默认显示的数据源
                If txtAppName.Text = "Slxt" Then
                    txtBizObjectRoot.Text = "ep_customer"
                    txtBizObjectPK.Text = "CstGUID"
                ElseIf txtAppName.Text = "Zlxt" Then
                    txtBizObjectRoot.Text = "ep_customer_zl"
                    txtBizObjectPK.Text = "CstGUID"
                ElseIf txtAppName.Text = "Hyxt" Then
                    txtBizObjectRoot.Text = "vp_BizMember"
                    txtBizObjectPK.Text = "BizID"
                End If

                '内容模板新增时候的数据来源是空
                txtBizObject.Text = ""


                '是否存在Type参数
                If Not Request.QueryString("Type") Is Nothing Then
                    txtType.Text = Request.QueryString("Type")
                End If
            End If

            '修改模式，往页面填写数据
            If txtMode.Text <> "1" Then
                txtActExecGUID.Text = Request.QueryString("Guid")

                DataToWeb()

                '编辑模式下只读优先级
                ddlPriority.Enabled = False

                If txtActType.Text = "短信" Then
                    crmHtmlBar.Options = HtmlBar.HtmlBarParam.CutCopyPaste
                End If

                If txtMode.Text = "3" Then
                    txtIsCreate.Text = ""
                End If
            End If

            '加载发送类型的页面状态
            If txtActType.Text = "短信" Then
                rbSMS.Checked = True
            Else
                rbEmail.Checked = True
            End If

            '绑定数据源
            myCstGroupDB.CreateBizObjectKHGH(ddlYwObject, Trim(txtAppName.Text))

            If txtMode.Text <> "1" Then
                ddlYwObject.Enabled = False
                Dim sBizObjectSQL = "SELECT BizObject+';'+BizObjectPK as BizObject FROM p_BizObjectSetting where bizobject in (select bizobject from vp_ActExecLog where ActExecGUID='" & txtActExecGUID.Text & "')"
                Dim sBizObject2PK As String = MyDB.GetDataItemString(sBizObjectSQL).ToString()
                ddlYwObject.SelectedValue = sBizObject2PK
                txtBizObjectRoot.Text = sBizObject2PK.Split(";")(0)
                txtBizObjectPK.Text = sBizObject2PK.Split(";")(1)
            End If

            If Request.QueryString("Type") = "es_Opportunity" Or Request.QueryString("Type") = "ep_customer" Then
                ddlYwObject.Enabled = False
                ddlYwObject.SelectedValue = "ep_Customer;CstGUID"
                txtBizObjectRoot.Text = "ep_Customer"
                txtBizObjectPK.Text = "CstGUID"
            ElseIf Request.QueryString("Type") = "vs_trade2cst" Then
                ddlYwObject.Enabled = False
                ddlYwObject.SelectedValue = "vs_trade2cst;Trade2CstGUID"
                txtBizObjectRoot.Text = "vs_trade2cst"
                txtBizObjectPK.Text = "Trade2CstGUID"
            ElseIf Request.QueryString("Type") = "vp_BizLead" Then
                ddlYwObject.Enabled = False
                ddlYwObject.SelectedValue = "vp_BizLead;BizID"
                txtBizObjectRoot.Text = "vp_BizLead"
                txtBizObjectPK.Text = "BizID"
            End If

            '绑定内容模板列表
            InitTemplate()

        End If

    End Sub

    '绑定内容模板列表
    Private Sub InitTemplate()
        Dim strSQL As String
        Dim dtTemp As DataTable
        strSQL = " SELECT cast(TemplateId as varchar(50))+'|'+isnull(case when scope='系统' then bizobject else BizObjectView end,'') as value,Title as text " & _
                 " FROM p_Template " & _
                 " WHERE TemplateType ='" & txtActType.Text & "' AND BUGUID = '" & Session("BUGUID") & "' AND AppName='" & txtAppName.Text & "' AND BizObject='" & txtBizObjectRoot.Text & "' AND (Scope='公共' OR Scope='系统' OR Scope='个人' AND Owner='" & Session("UserGUID") & "') "
        dtTemp = MyDB.GetDataTable(strSQL)
        ddlTemplete.DataTextField = "text"
        ddlTemplete.DataValueField = "value"
        ddlTemplete.DataSource = dtTemp
        ddlTemplete.DataBind()
        ddlTemplete.Items.Insert(0, "")
    End Sub

    '功能：将模板内容，由XML转成HTML
    '参数：XML
    '返回：HTML
    Private Function TransformHtml(ByVal xml As String) As String
        '将模板内容由XML转为HTML格式
        Dim XmlDoc As New XmlDocument
        XmlDoc.LoadXml(xml)

        Dim XslDoc As New Xsl.XslTransform
        XslDoc.Load(Page.MapPath("TransformHTML.xsl"))

        Dim Nav As XPath.XPathNavigator = XmlDoc.DocumentElement.CreateNavigator()
        Dim writer1 As New StringWriter
        XslDoc.Transform(Nav, Nothing, writer1, Nothing)

        '检查HTML标记是否安全
        Dim strHtml As String
        strHtml = Mysoft.WebControls.Util.BuildSafeUiHtml(writer1.ToString)
        strHtml = Mysoft.WebControls.Util.BuildSafeHtml(strHtml, True)

        Return strHtml
    End Function

    '绑定数据，暂时未对字段的合法性加判断
    Private Sub DataToWeb()
        Dim dtAct As DataTable
        Dim sSql As String
        sSql = "select * from vp_ActExecLog where ActExecGUID='" & txtActExecGUID.Text & "'"
        dtAct = MyDB.GetDataTable(sSql)
        If dtAct.Rows.Count > 0 Then
            txtBUGUID.Text = dtAct.Rows(0).Item("BUGUID").ToString()
            txtUserGUID.Text = dtAct.Rows(0).Item("UserGUID").ToString()
            txtExecutor.Text = dtAct.Rows(0).Item("Executor").ToString()
            If Not dtAct.Rows(0).Item("ExecDate") Is DBNull.Value Then
                txtExecDate.Value = Format(dtAct.Rows(0).Item("ExecDate"), "yyyy-MM-dd")
            End If

            txtAppName.Text = dtAct.Rows(0).Item("AppName").ToString()
            txtActType.Text = dtAct.Rows(0).Item("ActType").ToString()
            ddlPriority.SelectedValue = dtAct.Rows(0).Item("Priority").ToString()
            txtActExecName.Text = dtAct.Rows(0).Item("ActExecName").ToString()
            txtState.Text = dtAct.Rows(0).Item("State").ToString()
            txtBizObject.Text = dtAct.Rows(0).Item("BizObjectView").ToString()
            If Trim(dtAct.Rows(0).Item("TemplateContentXML").ToString()) <> "" Then
                Me._presentationHtml = TransformHtml(Trim(dtAct.Rows(0).Item("TemplateContentXML").ToString()))
            End If

            txtIsImport.Text = dtAct.Rows(0).Item("IsImport").ToString()
            If Not dtAct.Rows(0).Item("BeginDate") Is DBNull.Value Then
                txtBeginDate.Value = Format(dtAct.Rows(0).Item("BeginDate"), "yyyy-MM-dd")
            End If
            If Not dtAct.Rows(0).Item("EndDate") Is DBNull.Value Then
                txtEndDate.Value = Format(dtAct.Rows(0).Item("EndDate"), "yyyy-MM-dd")
            End If

            If dtAct.Rows(0).Item("SearchType").ToString() = "查询结果" Then
                RdoSearchType1.Checked = True
            Else
                RdoSearchType2.Checked = True
            End If

            '发送计划字段数据
            'AutoSendType	发送类型
            'SendMonthType	每月类型选择
            'AutoSendTime	发送时间（小时）
            'AutoSendMinute	发送时间（分钟）
            'SendWeek	星期数（复选）
            'RepeatDayOrWeek	重复天数/周数
            'SendMonth	月份（复选）
            'SendMonthToDay	发送日期（复选）
            'SendMonthToBgnWeek	第几周
            'AdvanceDay 生日提前几天

            txtTbAutoSendType.Text = dtAct.Rows(0).Item("AutoSendType").ToString()
            txtTbSendMonthType.Text = dtAct.Rows(0).Item("SendMonthType").ToString()
            txtTbAutoSendTime.Text = dtAct.Rows(0).Item("AutoSendTime").ToString()
            txtTbAutoSendMinute.Text = dtAct.Rows(0).Item("AutoSendMinute").ToString()
            txtTbSendWeek.Text = dtAct.Rows(0).Item("SendWeek").ToString()
            txtTbRepeatDayOrWeek.Text = dtAct.Rows(0).Item("RepeatDayOrWeek").ToString()
            txtTbSendMonth.Text = dtAct.Rows(0).Item("SendMonth").ToString()
            txtTbSendMonthToDay.Text = dtAct.Rows(0).Item("SendMonthToDay").ToString()
            txtTbSendMonthToBgnWeek.Text = dtAct.Rows(0).Item("SendMonthToBgnWeek").ToString()
            txtTbAdvanceDay.Text = dtAct.Rows(0).Item("AdvanceDay").ToString()

            txtFilterStru.Text = dtAct.Rows(0).Item("CstFilterStru").ToString()
            txtFilter.Text = dtAct.Rows(0).Item("CstFilter").ToString()

        End If

    End Sub

End Class